package com.ebook.ui.fragment.home.littlebookcase;

import com.ebook.entity.BookReadData;
import com.ebook.ui.fragment.Fragment;
import com.ebook.ui.sync.WorkSyncer;

import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RecentReadBook extends Fragment {
    private  List<BookReadData> data;
    private List<RecentBook> recentBooks = new ArrayList<>();
    public RecentReadBook(){
        super();
        initComponents();
        setBackground(Color.ORANGE);
        this.setBounds(0,50,120,340);
    }
    @Override
    public void initComponents(){
        //最多展示8个近期看过的图书
        //定义看过时可以采用严格定义,使得用户在看过超过若干页面是才会被认为是看过
        if(WorkSyncer.loginUser!=null){
           data = WorkSyncer.bookReadDataDao.selectDataByUserId(WorkSyncer.loginUser.getUserId());
           if(data.size()>4){
               int y = 0;
               for(int i = data.size()-1,j=4;j>=1;j--){
                   RecentBook recentBook = new RecentBook(data.get(i));
                   recentBook.setBounds(0,y,120,80);
                   y+=80;
                   add(recentBook);
                   this.recentBooks.add(recentBook);
               }
           }else{
               int y = 0;
               for(int i = data.size()-1;i>=0;i--){
                   RecentBook recentBook = new RecentBook(data.get(i));
                   recentBook.setBounds(0,y,120,80);
                   y+=80;
                   add(recentBook);
                   this.recentBooks.add(recentBook);
               }
           }
        }else{
            data = null;
        }

    }
}
